import {
  request,
  BASEURL
} from '../../utils/request';
Page({
  data: {
    index: 1,
    orderList: [],
    show: false,
    thumbUrl:getApp().globalData.thumbUrl
  },

  onLoad: function (options) {
    console.log(options)
  },

  onShow: function () {
    //判断是否登录
    var uid = wx.getStorageSync('uid')
    if (!uid) {
      // show 初始值为true 为false 登录之后也有几秒时间看见 请先登录
      return
    } else {
      //获取订单页面数据
      this.orderList()
      this.setData({
        show: true
      })
    }
  },

  //请求订单页面数据
  async orderList() {
    var that = this
    console.log(that.data.index)
    let {
      openid
    } = wx.getStorageSync('uid')
    let res = await request({
      url: '/api/Wxapp/myorder',
      method: "POST",
      data: {
        status: that.data.index,
        openid
      }
    })
    var code=res.data.code;
    if(code == 1){
      that.setData({
        show: true
      })
      that.setData({
        orderList: res.data.data
      })
    }else{

      that.setData({
          show: false
        })
      
    }
    
  },

  //确认收货
  async affirm(e) {
    console.log(e)
    let index = e.currentTarget.dataset.id
    console.log(index)
    // openid   oid(订单id)

    let {
      openid
    } = wx.getStorageSync('uid')
    let oid = this.data.orderList[index].id
    console.log(oid)
    let res = await request({
      url: '/api/Wxapp/confirmeOrder',
      method: "POST",
      data: {
        openid,
        oid
      }
    })
    wx.showToast({
      title: '收货成功',
      duration:1500
    })
    setTimeout(()=>{
    // 刷新页面
    this.orderList()
    },1500)

  },

  //切换
  tabIndex: function (e) {
    console.log(e)
   
    this.setData({
      index: e.currentTarget.dataset.index
    })
    this.orderList()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})